package org.luxor.common.netty.protocol.message;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import org.luxor.common.netty.protocol.NettyFixedHeader;
import org.luxor.common.netty.protocol.NettyMessageType;
import org.luxor.common.netty.protocol.NettyQoS;
import org.luxor.common.netty.protocol.NettySendVariableHeader;

/**
 * Netty Publish Message
 *
 * @author Mr.yan @date 2024/1/31
 */
public class NettyPublishMessage extends NettyMessage implements ByteBufHolder {

    public NettyPublishMessage(NettyFixedHeader fixedHeader, Object variableHeader, ByteBuf payload) {
        super(fixedHeader, variableHeader, payload);
    }

    @Override
    public NettySendVariableHeader variableHeader() {
        return (NettySendVariableHeader) super.variableHeader();
    }

    @Override
    public ByteBuf payload() {
        return content();
    }

    @Override
    public ByteBuf content() {
        return ByteBufUtil.ensureAccessible((ByteBuf) super.payload());
    }

    @Override
    public NettyPublishMessage copy() {
        return replace(content().copy());
    }

    @Override
    public NettyPublishMessage duplicate() {
        return replace(content().duplicate());
    }

    @Override
    public NettyPublishMessage retainedDuplicate() {
        return replace(content().retainedDuplicate());
    }

    @Override
    public NettyPublishMessage replace(ByteBuf content) {
        return new NettyPublishMessage(fixedHeader(), variableHeader(), content);
    }

    @Override
    public int refCnt() {
        return content().refCnt();
    }

    @Override
    public NettyPublishMessage retain() {
        content().retain();
        return this;
    }

    @Override
    public NettyPublishMessage retain(int increment) {
        content().retain(increment);
        return this;
    }

    @Override
    public NettyPublishMessage touch() {
        content().touch();
        return this;
    }

    @Override
    public NettyPublishMessage touch(Object hint) {
        content().touch(hint);
        return this;
    }

    @Override
    public boolean release() {
        return content().release();
    }

    @Override
    public boolean release(int decrement) {
        return content().release(decrement);
    }


    public static NettyPublishMessage fromQos0(String messageId, String messageName, String accessToken, ByteBuf payload) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, false, NettyQoS.AT_MOST_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName, accessToken);
        return new NettyPublishMessage(fixedHeader, variableHeader, payload);
    }

    public static NettyPublishMessage fromQos0(String messageId, String messageName, ByteBuf payload) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, false, NettyQoS.AT_MOST_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName);
        return new NettyPublishMessage(fixedHeader, variableHeader, payload);
    }

    public static NettyPublishMessage fromQos0(String messageId, String messageName) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, false, NettyQoS.AT_MOST_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName);
        return new NettyPublishMessage(fixedHeader, variableHeader, null);
    }

    public static NettyPublishMessage fromQos1(String messageId, String messageName, boolean isDup, String accessToken, ByteBuf payload) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, isDup, NettyQoS.AT_LEAST_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName, accessToken);
        return new NettyPublishMessage(fixedHeader, variableHeader, payload);
    }

    public static NettyPublishMessage fromQos1(String messageId, String messageName, boolean isDup, ByteBuf payload) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, isDup, NettyQoS.AT_LEAST_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName);
        return new NettyPublishMessage(fixedHeader, variableHeader, payload);
    }

    public static NettyPublishMessage fromQos1(String messageId, String messageName, boolean isDup) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, isDup, NettyQoS.AT_LEAST_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName);
        return new NettyPublishMessage(fixedHeader, variableHeader, null);
    }

    public static NettyPublishMessage fromQos2(String messageId, String messageName, boolean isDup, String accessToken, ByteBuf payload) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, isDup, NettyQoS.EXACTLY_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName, accessToken);
        return new NettyPublishMessage(fixedHeader, variableHeader, payload);
    }

    public static NettyPublishMessage fromQos2(String messageId, String messageName, boolean isDup, ByteBuf payload) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, isDup, NettyQoS.EXACTLY_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName);
        return new NettyPublishMessage(fixedHeader, variableHeader, payload);
    }

    public static NettyPublishMessage fromQos2(String messageId, String messageName, boolean isDup) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUBLISH, isDup, NettyQoS.EXACTLY_ONCE);
        NettySendVariableHeader variableHeader = NettySendVariableHeader.from(messageId, messageName);
        return new NettyPublishMessage(fixedHeader, variableHeader, null);
    }

}
